package com.yiwei.hdb.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.hdb.R;
import com.yiwei.hdb.activity.HelpActivity;
import com.yiwei.hdb.activity.MainActivityNew;
import com.yiwei.hdb.activity.V2MyQrcodeActivity;
import com.yiwei.hdb.activity.V3BankMoreListActivity;
import com.yiwei.hdb.activity.V3IdcardIdentityActivity;
import com.yiwei.hdb.activity.V3MyBorrowRecordActivity;
import com.yiwei.hdb.activity.V3MyFundActivity;
import com.yiwei.hdb.activity.V3MySettingActivity;
import com.yiwei.hdb.api.callback.UserContract;
import com.yiwei.hdb.api.requestInfo.UserRequest;
import com.yiwei.hdb.constant.UserGlobal;
import com.yiwei.hdb.event.ChangeHomePagerEvent;
import com.yiwei.hdb.model.UserModel;
import com.yiwei.hdb.util.PreferenceUtils;
import com.yiwei.hdb.util.Spans;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.util.Util;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-7-27.
 */

public class V3MyFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, UserContract.View {
    @BindView(R.id.view_head)
    View viewHead;
    @BindView(R.id.txt_name)
    TextView txtName;
    @BindView(R.id.txt_level)
    SuperButton txtLevel;
    @BindView(R.id.txt_is_realname)
    SuperButton txtIsRealname;
    @BindView(R.id.left_img)
    ImageButton leftImg;
    @BindView(R.id.txt_edu)
    TextView txtEdu;
    //    @BindView(R.id.txt_edu_copy)
//    TextView txtEduCopy;

    @BindView(R.id.txt_money_need_pay)
    TextView txtMoneyNeedPay;
    @BindView(R.id.btn_loan_or_borrow)
    SuperButton btnLoanOrBorrow;
    @BindView(R.id.layout_login)
    RelativeLayout layoutLogin;
    @BindView(R.id.btn_my_fund)
    SuperButton btnMyFund;
    @BindView(R.id.btn_my_borrow)
    SuperButton btnMyBorrow;
    @BindView(R.id.btn_sign)
    RelativeLayout btnSign;
    @BindView(R.id.btn_point)
    RelativeLayout btnPoint;
    @BindView(R.id.btn_youhui)
    RelativeLayout btnYouhui;
    @BindView(R.id.btn_invite)
    RelativeLayout btnInvite;
    @BindView(R.id.btn_setting)
    RelativeLayout btnSetting;
    @BindView(R.id.scrollView)
    NestedScrollView scrollView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.layout_total)
    LinearLayout layoutTotal;
    Unbinder unbinder;
    @BindView(R.id.txt_edu_can_use)
    TextView txtEduCanUse;
    @BindView(R.id.layout_info)
    LinearLayout layoutInfo;
    @BindView(R.id.view_bottom)
    View viewBottom;
    @BindView(R.id.btn_bank)
    RelativeLayout btnBank;
    @BindView(R.id.txt_version)
    TextView txtVersion;
    @BindView(R.id.img_setting_right)
    ImageView imgSettingRight;
    private UserRequest api;
    private int userType = -1;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_my_v3;
    }

    @Override
    protected void initView() {
        api = UserRequest.create();
        api.attachView(this);
//        EventBus.getDefault().register(this);
        refresh.setOnRefreshListener(this);
        txtVersion.setText("v"+Util.getVersion(getContext()));
    }

    @Override
    public void onResume() {
        super.onResume();
        if (UserGlobal.checkLogin()) {
            api.getUserInfo();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        api.detachView();
        api = null;
        unbinder.unbind();
    }

    @OnClick({R.id.btn_bank, R.id.left_img, R.id.btn_loan_or_borrow, R.id.btn_my_fund, R.id.btn_my_borrow, R.id.btn_sign, R.id.btn_point, R.id.btn_youhui, R.id.btn_invite, R.id.btn_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_img:
                Intent msgIntent = new Intent(getActivity(), HelpActivity.class);
                msgIntent.putExtra("type", 0);
                startActivity(msgIntent);
                getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
            case R.id.btn_loan_or_borrow:
                if (userType == 0) {
//                    Util.startActivity(getContext(), V3RepayMoneyActivity.class);
                    ((MainActivityNew) getActivity()).getHuaKouInfo();

                }
                break;
            case R.id.btn_bank:
                if (Util.checkLogin(getActivity())){
                    if (!UserGlobal.getIsIdcardPass()) {
                        ToastUtil.makeText(getContext(), "请先完成实名");
                        Util.startActivity(getContext(), V3IdcardIdentityActivity.class);
                        return;
                    }
                    Util.startActivity(getActivity(), V3BankMoreListActivity.class);
                }

                break;
            case R.id.btn_my_fund:
                if (Util.checkLogin(getActivity()))
                    Util.startActivity(getActivity(), V3MyFundActivity.class);
                break;
            case R.id.btn_my_borrow:
                if (Util.checkLogin(getActivity()))
                    Util.startActivity(getActivity(), V3MyBorrowRecordActivity.class);
                break;

            case R.id.btn_sign:
//                ToastUtil.makeText(getContext(), "敬请期待");
                if (Util.checkLogin(getActivity())) {
                    EventBus.getDefault().post(new ChangeHomePagerEvent(1));
                }
                break;
            case R.id.btn_point:
                ToastUtil.makeText(getContext(), "敬请期待");
                break;
            case R.id.btn_youhui:
                ToastUtil.makeText(getContext(), "敬请期待");
                break;
            case R.id.btn_invite:
                if (Util.checkLogin(getActivity())) {
                    Util.startActivity(getActivity(), V2MyQrcodeActivity.class);
                }
                break;
            case R.id.btn_setting:
                if (Util.checkLogin(getActivity())) {
                    Util.startActivity(getActivity(), V3MySettingActivity.class);
                }
                break;
        }
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        if (refresh != null && refresh.isRefreshing()) {
            refresh.setRefreshing(false);
        }
    }

    @Override
    public void onRequestStart() {
        refresh.setRefreshing(true);
    }

    @Override
    public void getUserInfoSuccess(UserModel model) {
        if (model.getStatus() == 0) {
            layoutLogin.setVisibility(View.GONE);

        } else if (model.getStatus() == 98) {
            ((MainActivityNew) getActivity()).gotoUpdate();
        } else if (model.getStatus() == 1) {
            layoutLogin.setVisibility(View.VISIBLE);
            setUserData(model);
        }
    }

    private void setUserData(UserModel model) {
        UserGlobal.saveUserInfo(model);
        txtLevel.setText("LV" + model.level_id);
        if (model.getIdcardpassed().equals("0")) {
            txtIsRealname.setText("未实名");
            String mobile = model.getMobile();
            if (Util.isNumeric(mobile) && mobile.length() == 11)
                txtName.setText(Util.replaceString(mobile, 5, 4, '*'));
        } else {
            txtIsRealname.setText("已实名");
            txtName.setText(model.getReal_name());
        }


        if (PreferenceUtils.getInt(getContext(), "imposeDay", 0) > 0) {
            userType = 0;

            btnLoanOrBorrow.setVisibility(View.VISIBLE);
        } else {
            btnLoanOrBorrow.setVisibility(View.GONE);

            if (!TextUtils.isEmpty(PreferenceUtils.getString(getContext(), "repayMoney", ""))) {
                userType = 0;
            } else {
                userType = 1;
//                String canUse = TextUtils.isEmpty(model.getMoney()) ? "" : model.getMoney().substring(0, model.getMoney().indexOf('.')).replace("￥", "");

            }
        }
        if (Float.valueOf(model.getRepay_money().replace("￥", "").replace(",", "")) != 0) {
            btnLoanOrBorrow.setVisibility(View.VISIBLE);
            btnLoanOrBorrow.setText("去还款");
        } else {
            btnLoanOrBorrow.setVisibility(View.GONE);
        }


        Spanned EduCanUse = Spans.with(getContext()).font(model.getMoney(), getResources().getDimensionPixelSize(R.dimen.x36), 0xff616185).font("\n可借额度(元)", getResources().getDimensionPixelSize(R.dimen.x24), 0xff616185).build();
        txtEduCanUse.setText(EduCanUse);
        Spanned MoneyNeedPay = Spans.with(getContext()).font(model.getRepay_money().replace("￥", ""), getResources().getDimensionPixelSize(R.dimen.x36), 0xff616185).font("\n近期待还(元)", getResources().getDimensionPixelSize(R.dimen.x24), 0xff616185).build();
        txtMoneyNeedPay.setText(MoneyNeedPay);
//        txtEdu.setText(model.money_amount);

        Spanned Edu = Spans.with(getContext()).font(model.money_amount, getResources().getDimensionPixelSize(R.dimen.x60), 0xff616185).font("\n总额度(元)", getResources().getDimensionPixelSize(R.dimen.x24), 0xff616185).build();
        txtEdu.setText(Edu);
    }

    @Override
    public void onRefresh() {
        api.getUserInfo();
    }
}
